package com.ypd.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ypd.common.utils.SecurityUtils;
import com.ypd.shop.domain.AuctionEnrollment;
import com.ypd.shop.domain.AuctionStatus;
import com.ypd.shop.domain.Bid;
import com.ypd.shop.domain.dto.AuctionDto;
import com.ypd.shop.domain.vo.AuctionVo;
import com.ypd.shop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ypd.shop.mapper.AuctionItemMapper;
import com.ypd.shop.domain.AuctionItem;
import org.springframework.transaction.annotation.Transactional;

/**
 * 拍卖品基础信息Service业务层处理
 * 
 * @author lin
 * @date 2025-02-28
 */
@Service
public class AuctionItemServiceImpl implements IAuctionItemService 
{
    @Autowired
    private AuctionItemMapper auctionItemMapper;
    @Autowired
    private IAuctionStatusService auctionStatusService;
    @Autowired
    private IBidService bidService;
    @Autowired
    private IHouseDescriptionService houseDescriptionService;
    @Autowired
    private IAuctionEnrollmentService auctionEnrollmentService;

    /**
     * 查询拍卖品全部信息
     * 
     * @param itemId 拍卖品基础信息主键
     * @return 拍卖品基础信息
     */
    @Override
    public AuctionVo selectAuctionByItemId(String itemId)
    {
        //进行修改满足Vo
        AuctionItem item = auctionItemMapper.selectAuctionItemByItemId(itemId);
        AuctionVo auctionVo = new AuctionVo();
        //使用当前价和动态调整的预计结束时间
        AuctionStatus auctionStatus = auctionStatusService.selectAuctionStatusByItemId(itemId);
        item.setStartPrice(auctionStatus.getCurrentPrice());
        item.setEndTime(auctionStatus.getEndTime());
        auctionVo.setAuctionItem(item);
        auctionVo.setHouseDescription(houseDescriptionService.selectHouseDescriptionByItemId(itemId));
        auctionVo.setBids(bidService.selectBidList(Bid.builder().itemId(itemId).build()));
        //查看该用户是否已经报名这个拍卖活动
        auctionVo.setEnrolled(auctionEnrollmentService.selectAuctionEnrollmentList
                (AuctionEnrollment.builder().userId(String.valueOf(SecurityUtils.getUserId())).itemId(itemId).build()).size()>0);
        return auctionVo;
    }
    /**
     * 查询拍卖品基本信息
     *
     * @param itemId 拍卖品基础信息主键
     * @return 拍卖品基础信息
     */
    @Override
    public AuctionItem selectAuctionItemByItemId(String itemId)
    {
        return auctionItemMapper.selectAuctionItemByItemId(itemId);
    }

    /**
     * 用户端查询拍卖品基础信息列表
     * 
     * @param auctionItem 拍卖品基础信息
     * @return 拍卖品基础信息
     */
    @Override
    public List<AuctionItem> selectAuctionItemList(AuctionItem auctionItem)
    {
        ArrayList<Long> list = new ArrayList<>();
        list.add(1L);
        list.add(2L);
        auctionItem.getParams().put("status",list);
        List<AuctionItem> items = auctionItemMapper.selectAuctionItemList(auctionItem);
        for (int i = 0; i < items.size(); i++) {
            AuctionItem item = items.get(i);
                //使用当前价和动态调整的预计结束时间
                AuctionStatus auctionStatus = auctionStatusService.selectAuctionStatusByItemId(items.get(i).getItemId());
                item.setStartPrice(auctionStatus.getCurrentPrice());
                item.setEndTime(auctionStatus.getEndTime());
                if (item.getImage() != null){
                    item.setImage(items.get(i).getImage().split(",")[0]);
                }
        }
        return items;
    }
    /**
     * 管理员端查询拍卖品基础信息列表
     *
     * @param auctionItem 拍卖品基础信息
     * @return 拍卖品基础信息
     */
    @Override
    public List<AuctionItem> selectAuctionItemListByAdmin(AuctionItem auctionItem)
    {
        //直接查询所有的
        List<AuctionItem> items = auctionItemMapper.selectAuctionItemList(auctionItem);
        for (int i = 0; i < items.size(); i++) {
            AuctionItem item = items.get(i);
            //使用当前价和动态调整的预计结束时间
            AuctionStatus auctionStatus = auctionStatusService.selectAuctionStatusByItemId(items.get(i).getItemId());
            item.setStartPrice(auctionStatus.getCurrentPrice());
            item.setEndTime(auctionStatus.getEndTime());
        }
        return items;
    }


    /**
     * 新增拍卖品基础信息
     * 多表一起新增
     * 
     * @param dto 拍卖品基础信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAuctionItem(AuctionDto dto)
    {
        //item,status,hourse表一起新增
        AuctionItem item = dto.getItem();
        auctionItemMapper.insertAuctionItem(item);//insert之后返回主键
        AuctionStatus status = AuctionStatus.builder().currentPrice(item.getStartPrice()).endTime(item.getEndTime()).currentPrice(item.getStartPrice()).itemId(item.getItemId()).build();
        auctionStatusService.insertAuctionStatus(status);
        dto.getHouse().setItemId(item.getItemId());
        houseDescriptionService.insertHouseDescription(dto.getHouse());
        return 1;
    }

    /**
     * 修改拍卖品基础信息
     * 
     * @param auctionItem 拍卖品基础信息
     * @return 结果
     */
    @Override
    public int updateAuctionItem(AuctionItem auctionItem)
    {
        return auctionItemMapper.updateAuctionItem(auctionItem);
    }

    /**
     * 批量删除拍卖品基础信息
     * 
     * @param itemIds 需要删除的拍卖品基础信息主键
     * @return 结果
     */
    @Override
    public int deleteAuctionItemByItemIds(String[] itemIds)
    {
        return auctionItemMapper.deleteAuctionItemByItemIds(itemIds);
    }

    /**
     * 删除拍卖品基础信息信息
     * 
     * @param itemId 拍卖品基础信息主键
     * @return 结果
     */
    @Override
    public int deleteAuctionItemByItemId(String itemId)
    {
        return auctionItemMapper.deleteAuctionItemByItemId(itemId);
    }
}
